package com.peanut.baby.mvp.livedetail.livegroupdetail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.LiveGroup;
import com.peanut.baby.model.LiveGroupRel;
import com.peanut.baby.model.LiveMsg;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.livedetail.LiveDetailActivity;
import com.peanut.baby.mvp.livedetail.group.LiveDetailGroupContract;
import com.peanut.baby.mvp.livedetail.group.LiveDetailGroupPresenter;
import com.peanut.baby.mvp.liveroom.AppVoicePlayer;
import com.peanut.baby.mvp.liveroom.LiveRoomActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.pullrecycle.BaseRecyclerAdapter;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/8/2.
 */

public class LiveGroupTabFragment extends BaseMvpFragment implements BaseRecyclerAdapter.OnRecyclerItemClickListener, LiveDetailGroupContract.View {
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecycler;
    @BindView((R.id.enter_room))
    TextView enter_room;

    Unbinder unbinder;
    private List<LiveGroup> comments;
    private LiveGroupTabRecyclerAdapter adapter;
    private LiveDetailGroupPresenter presenter;
    private int tabType = 0;//1 我的 已报名 0全部系列课
    private LiveGroup currPlaying;

    public static LiveGroupTabFragment newInstance(ArrayList<LiveGroup> groupList, int type) {
        LiveGroupTabFragment fg = new LiveGroupTabFragment();
        Bundle bundle = new Bundle();
        fg.setArguments(bundle);
        bundle.putSerializable("liveRoom", groupList);
        bundle.putInt("type", type);
        return fg;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_livegroup_detail_group, null);
        unbinder = ButterKnife.bind(this, rootView);
        pullRecycler.enablePullRefresh(false);
        pullRecycler.enableLoadMore(false);
//        pullRecycler.setOnRecyclerRefreshListener(this);
        return rootView;
    }

    @Override
    public void initData() {
        comments = (List<LiveGroup>) getArguments().getSerializable("liveRoom");
        tabType = getArguments().getInt("type");
        presenter = new LiveDetailGroupPresenter(this, getActivity());
        adapter = new LiveGroupTabRecyclerAdapter(getActivity(), comments, tabType);
        pullRecycler.setLayoutManager(new XLinearLayoutManager(getActivity()));
        pullRecycler.setAdapter(adapter);
        adapter.setOnRecyclerItemClickListener(this);
        enter_room.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRequestFailed(String code, String reason) {
        dismissProgress();
    }

    @Override
    public void onJoinSuccess(String ids) {

    }

    @Override
    public void onMessageListGet(List<LiveMsg> list) {
        dismissProgress();
        List<String> audioList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (LiveMsg msg : list) {
                if (msg.messageType == 3)
                    audioList.add(msg.getMessageContent().url);
            }
        }
        System.out.println(audioList.size() + "音频----size");
        if (audioList.size() > 0) {
            AppVoicePlayer.getInstance().playList(audioList, new AppVoicePlayer.OnAppVoicePlayListener() {
                @Override
                public void onException(String reason) {
                    Toast.makeText(getActivity(), "音频播放出错", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPlayFinished() {
                    if (currPlaying != null) {
                        currPlaying.playStatus = 0;
                        adapter.notifyDataSetChanged();
                        InitManager.getInstance().playingId = 0;
                    }
                }

                @Override
                public void onPlayStarted() {
                }

                @Override
                public void onInterrupt() {
                }

            });

        } else {
            Toast.makeText(getActivity(), "该直播没有音频", Toast.LENGTH_SHORT).show();
            if (currPlaying != null) {
                currPlaying.playStatus = 0;
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onLiveRoomsGet(List<LiveGroupRel> list) {
        if (list != null && list.size() > 0) {
            LiveGroupRel rel = list.get(0);
            LiveDetailActivity.start(getActivity(), rel.liveroomId, 1);
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        LiveGroup group = comments.get(position);
        int playStatus = group.playStatus;
        if (group.isEnrole == 1) {
            if (playStatus == 1 && AppVoicePlayer.getInstance().isPlaying()) {
                AppVoicePlayer.getInstance().releasePlayer();
                currPlaying = null;
                InitManager.getInstance().playingId = 0;
                group.playStatus = 0;
                adapter.notifyDataSetChanged();
            } else {
                if (currPlaying != null) {
                    currPlaying.playStatus = 0;
                }

                //开始处理播放
                showProgress("获取播放列表...", false);
                presenter.messageListGetByGroupId(comments.get(position).id + "");
                currPlaying = group;
                InitManager.getInstance().playingId = group.id;
                group.playStatus = 1;
                adapter.notifyDataSetChanged();
            }
        } else {
            presenter.getLiveRoomsByGroupId(comments.get(position).id + "");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        InitManager.getInstance().playingId = 0;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (comments != null && comments.size() > 0 && InitManager.getInstance().playingId != 0) {
                for (LiveGroup group : comments) {
                    if (group.id == InitManager.getInstance().playingId) {
                        group.playStatus = 1;
                    }
                }
            }
            if (adapter != null)
                adapter.notifyDataSetChanged();
        } else {
            if (comments != null && comments.size() > 0 && InitManager.getInstance().playingId != 0) {
                for (LiveGroup group : comments) {
                    group.playStatus = 0;
                }
            }
            if (adapter != null)
                adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        InitManager.getInstance().playingId = 0;
    }
}
